(function() {
	jQuery.fn.springLemonDialog = function(options) {
		var defaults = {
			title : '系统提示',
			content : "",
			url : "",
			showCloseButton : false,
			otherButtons : [ "确定", "取消" ],
			otherButtonStyles : [ 'btn-primary', 'btn-primary' ],
			bootstrapModalOption : {
				keyboard : true
			},
			dialogShow : function() {
			},
			dialogShown : function() {
			},
			dialogHide : function() {
			},
			dialogHidden : function() {
			},
			clickButton : function(sender, modal, index) {
			}
		};
		var options = jQuery.extend(defaults, options);
		var modalID = "";

		function getModalID() {
			var d = new Date();
			var vYear = d.getFullYear();
			var vMon = d.getMonth() + 1;
			var vDay = d.getDate();
			var h = d.getHours();
			var m = d.getMinutes();
			var se = d.getSeconds();
			var sse = d.getMilliseconds();
			return "t_" + vYear + vMon + vDay + h + m + se + sse
		}

		jQuery.fn.extend({
			closeDialog : function(modal) {
				var modalObj = modal;
				modalObj.modal("hide")
			}
		});
		return this
				.each(function() {
					var obj = jQuery(this);
					modalID = getModalID();
					jQuery(this).attr("data-target", modalID);
					var tmpHtml = '<div class="modal fade text-center" id="{ID}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel_{ID}" aria-hidden="true">'
							+ '<div class="modal-dialog">'
							+ '<div class="modal-content">'
							+ '<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button>'
							+ '<h4 class="modal-title" id="myModalLabel_{ID}">{title}</h4></div>'
							+ '<div class="modal-body" id="body_{ID}" style="overflow-y: auto;max-height: 600px;">{body}</div>'
							+ '<div class="modal-footer align-center">{button}</div></div></div></div>';
					var buttonHtml = '<button class="btn btn-primary" data-dismiss="modal" aria-hidden="true">关闭</button>';
					if (!options.showCloseButton
							&& options.otherButtons.length > 0) {
						buttonHtml = ""
					}
					var btnClass = "cls-" + modalID;
					for (var i = 0; i < options.otherButtons.length; i++) {
						buttonHtml += '<button buttonIndex="' + i + '" class="'
								+ btnClass + " btn "
								+ options.otherButtonStyles[i] + '">'
								+ options.otherButtons[i] + "</button>"
					}
					tmpHtml = tmpHtml.replace(/{ID}/g, modalID).replace(
							/{title}/g, options.title).replace(/{body}/g,
							options.content).replace(/{button}/g, buttonHtml);
					jQuery(obj).append(tmpHtml);
					var modalObj = jQuery("#" + modalID);
					jQuery("." + btnClass).click(function() {
						var index = jQuery(this).attr("buttonIndex");
						options.clickButton(jQuery(this), modalObj, index)
					});
					modalObj.on("show.bs.modal", function() {
						if (options.url != "") {
							jQuery("#ajax-loader").show();
							jQuery.ajax({
								type : "get",
								url : options.url,
								data : "dialogModalId=" + modalID
										+ "&ajaxloader=ajax-loader",
								success : function(data) {
									jQuery("#ajax-loader").hide();
									jQuery("#body_" + modalID).html(data)
								},
								error : function(e) {
									jQuery("#ajax-loader").hide();
									jQuery("#body_" + modalID).html("加载失败!")
								}
							})
						} else {
							jQuery("#body_" + modalID).html(options.content)
						}
						options.dialogShow()
					});
					modalObj.on("shown.bs.modal", function() {
						options.dialogShown()
					});
					modalObj.on("hide.bs.modal", function() {
						options.dialogHide()
					});
					modalObj.on("hidden.bs.modal", function() {
						options.dialogHidden();
						obj.removeAttr("data-target");
						modalObj.remove()
					});
					modalObj.modal(options.bootstrapModalOption)
				});// return this.each
	};

	jQuery.extend({
		springLemonDialog : function(options) {
			jQuery("body").springLemonDialog(options);
		}
	});

	jQuery.extend({
		springLemonMsgDialog : function(options) {
			var options = jQuery.extend(options, {
				showCloseButton : true,
				otherButtons : []
			});
			jQuery("body").springLemonDialog(options);
		}

	});
	jQuery.extend({
		slShowLoading : function() {
			jQuery('#loadingImage').show();
		}
	});
	jQuery.extend({
		slHideLoading : function() {
			jQuery('#loadingImage').hide();
		}
	});
	jQuery.getUrlParam = function(name) {
		var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|jQuery)");
		var r = window.location.search.substr(1).match(reg);
		if (r != null)
			return unescape(r[2]);
		return null;
	};

	// 加载图标设置
	jQuery(document).ajaxStart(function() {
		{
			jQuery('#loadingImage').show();
		}
	});

	jQuery(document).ajaxSuccess(function() {
		jQuery('#loadingImage').hide();
	});

	// 左边菜单点击处理
	$(function() {
		_.forEach($('.sidebar-menu ul,li'), function(item) {
			var $item = $(item);
			$item.removeClass('active');
		});

		var url = window.location.pathname;

		// 设置打开菜单和选定项

		if (url) {
			_.forEach(
							$('.sidebar-menu a'),
							function(item) {
								var $item = $(item);
								var itempath=$item.attr('href').trim();						
								if (url==itempath) {
									if ($item.parent('li').length > 0) {
										if ($item.parent('li').parent('ul').length > 0) {
											$item.parent('li').parent('ul')
													.addClass(
															'active menu-open')
													.css('display', 'block');

											$item.parent('li').addClass(
													'active');
										}

									}
									var parent = $item.parent('li')
											.parent('ul');
									if (parent.length > 0) {
										parent.addClass('active menu-open')
												.css('display', 'block');
										parent.parent('li').parent('ul')
												.addClass('active menu-open')
												.css('display', 'block');
									}
									return false;
								}
							});

		}

	});
	
	
	
/*
 *数组的集合操作
 */
	function array_remove_repeat(a) { // 去重
	    var r = [];
	    for(var i = 0; i < a.length; i ++) {
	        var flag = true;
	        var temp = a[i];
	        for(var j = 0; j < r.length; j ++) {
	            if(temp === r[j]) {
	                flag = false;
	                break;
	            }
	        }
	        if(flag) {
	            r.push(temp);
	        }
	    }
	    return r;
	}

	function array_intersection(a, b) { // 交集
	    var result = [];
	    for(var i = 0; i < b.length; i ++) {
	        var temp = b[i];
	        for(var j = 0; j < a.length; j ++) {
	            if(temp === a[j]) {
	                result.push(temp);
	                break;
	            }
	        }
	    }
	    return array_remove_repeat(result);
	}

	function array_union(a, b) { // 并集
	    return array_remove_repeat(a.concat(b));
	}

	array_difference=function (a, b) { // 差集 a - b
	    //clone = a
	    var clone = a.slice(0);
	    for(var i = 0; i < b.length; i ++) {
	        var temp = b[i];
	        for(var j = 0; j < clone.length; j ++) {
	            if(temp === clone[j]) {
	                //remove clone[j]
	                clone.splice(j,1);
	            }
	        }
	    }
	    return array_remove_repeat(clone);
	}	
	
   window.message=function message(msg){
		jQuery.springLemonDialog({
			content:msg,
			otherButtons:["确定"],
			clickButton:function(sender,modal,index){ $(this).closeDialog(modal);}
		});
   }
   
   

   



	
})();
